import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/motorcade_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/flexible_icon_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'select_motorcade_controller.dart';

class SelectMotorcadePage extends StatelessWidget {
  final controller = Get.put(SelectMotorcadeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "选择车队"),
      body: GetBuilder<SelectMotorcadeController>(
        builder: (controller) {
          return MultiStateWidget(
            child: ListView.builder(
              physics: BouncingScrollPhysics(),
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return buildItemWidget(index);
              },
              itemCount: controller.motorcadeList.length,
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }

  Widget buildItemWidget(index) {
    MotorcadeEntity entity = controller.motorcadeList[index];
    List<String> subCompanyList = entity.subCompanyList ?? [];
    return Container(
      margin: EdgeInsets.only(left: 16.w, top: index == 0 ? 12.w : 0, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              String id = entity.id ?? "";
              if (id.isEmpty) {
                return;
              }
              Get.back(result: {Constant.PARAM_MOTORCADE_ID: id, Constant.PARAM_MOTORCADE_NAME: entity.name ?? ""});
            },
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    entity.name ?? "",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  Text(
                    entity.code ?? "",
                    style: TextStyle(
                      color: Colours.color_cfcfd5,
                      fontSize: 12.sp,
                    ),
                  ),
                  SizedBox(
                    height: subCompanyList.isNotEmpty ? 7.w : 0,
                  ),
                  FlexibleIconText(
                    icon: Image.asset(
                      "images/ic_company.png",
                      height: 12.w,
                      width: 12.w,
                    ),
                    textList: subCompanyList,
                    iconTextPadding: 5.w,
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
